// pages/home/home.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navIdx: '1',
    id: '',
    btmList: [
      { img1: '/img/card-logo1.png', img: '/img/card-logo.png', title: '名片' },
      { img1: '/img/card-home1.png', img: '/img/card-home.png', title: '官网' },
    ],
    productList:[
     
    ],
    markers: [{
      width: 20,
      height: 35,
      longitude: '117.28',
      latitude: '31.86',
      iconPath: '/img/mark.png',
    }]
  },


  selectedNavTap: function (e) { //底部跳转
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    console.log(idx);
    if (idx == that.data.navIdx) {
      return;
    }
    that.setData({
      navIdx: idx
    })
    if (idx == 0) { //名片
      wx.redirectTo({
        url: '/pages/my-card/my-card',
      })
    } else if (idx == 1) { //官网
      wx.redirectTo({
        url: '/pages/home/home',
      })
    } else { //聊天
      wx.redirectTo({
        url: '',
      })
    }
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHome(); //获取官网信息
  },

  getHome() {//获取官网信息
    var that = this
    wx.request({
      url: app.globalData.host + 'website/index',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        user_id:wx.getStorageSync('userId')
      },
      success(res) {
        console.log(res)
        if (!res.data.data.content){
          wx.showModal({
            title: '温馨提示',
            content: '您的官网还未创建,是否创建？',
            showCancel: true,
            confirmText: '创建',
            success: function(e) {
              if(e.confirm){
                wx.navigateTo({
                  url: '/pages/home-add/home-add',
                })
              }else if (e.cancel){
                wx.navigateBack({
                  delta: 1,
                })
              }
            },
            fail: function(res) {},
            complete: function(res) {},
          })
        }
        console.log(JSON.parse(res.data.data.content))
        var content = JSON.parse(res.data.data.content)
        var target = [];
        var target = content.target.split(' ');
        // target0 = target0.toString();
        // target = target0.split(',');
        that.setData({
          add_comp_logo: content.add_comp_logo,
          add_banner_img: content.add_banner_img,
          add_about_img: content.add_about_img,
          add_honour_img: content.add_honour_img,
          target: target,
          aboutText: content.aboutText,
          awardText: content.awardText,
          phone: content.phone,
          addressName: content.addressName,
          addressDetail: content.addressDetail,
          latitude: content.latitude,
          longitude: content.longitude,
          'markers[0].latitude': content.latitude,
          'markers[0].longitude': content.longitude,
          company_id: res.data.data.id,

          user:res.data.data.member,
          product:res.data.data.product.data,
        })
      },
      fail(err) { },
      complete(comp) { },
    })

  },


  openProductDetail: function (e) { //打开产品详情
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/product-detail/product-detail?id=' + id,
    })
  },

  lookCardTap:function(){ //查看名片
    wx.navigateTo({
      url: '/pages/my-card/my-card',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
})